<?php
class Product 
{
	private $CI;
	
	public $id;
	public $name;
	public $description;
	public $image_ids;
	public $netto_price;
	public $price;
	public $profit_key;
	public $category_id;
	public $isFeatured;
	public $o_cat_name;

    /*  */
    public $images;

	function __construct($id, $name, $description, $images, $price, $category_id, $o_cat_name, $isFeatured) {
		$this->CI =& get_instance();

        $this->id = $id;
        $this->name = $name;
        $this->description = $description;
        $this->images = $images;
        $this->price = price;
        $this->category_id = $category_id;
        $this->o_cat_name = $o_cat_name;
        $this->isFeatured = $isFeatured;
	}
	
	public function update() {
		$data = array(
            'name' => $this->name,
			'description' => $this->description,
			'image_ids' => $this->image_ids,
			'netto_price' => $this->netto_price,
			'price' => $this->price,
			'profit_key' => $this->profit_key,
			'category_id' => $this->category_id,
			'isFeatured' => $this->isFeatured
        );
		
		$this->CI->db->where('id', $this->id);
		$res = $this->CI->db->update(TABLE_IMAGES, $data);
		
		if (!$res) {
		  $msg = $this->db->_error_message();
		  KLogger::Instance()->logDebug("Products->update() sql error: " . $msg);
		}
	}
	
	public function delete() {
		$res = $this->CI->db->delete(TABLE_IMAGES, array('id' => $this->id));
		
		if (!$res) {
		  $msg = $this->db->_error_message();
		  //$num = $this->db->_error_number();
		  
		  KLogger::Instance()->logDebug("Products->delete() sql error: " . $msg);
		}
	}

	public function upload() {
		$data = array(
            'name' => $this->name,
			'description' => $this->description,
			'image_ids' => $this->image_ids,
			'netto_price' => $this->netto_price,
			'price' => $this->price,
			'profit_key' => $this->profit_key,
			'category_id' => $this->category_id,
			'isFeatured' => $this->isFeatured
        );

		$this->CI->db->insert(TABLE_IMAGES, $data);
	}
	
	public function toJSON() {
		return json_encode($this);
	}
}
?>